package com.lhy.gulimall.product.web;

import com.lhy.gulimall.product.entity.CategoryEntity;
import com.lhy.gulimall.product.service.CategoryService;
import com.lhy.gulimall.product.vo.Catelog2Vo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@Slf4j
public class IndexController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping({"/", "/index.html"})
    public String indexPage(Model model) {
        // TODO 查询所有一级分类
        long start = System.currentTimeMillis();
        List<CategoryEntity> categorys = categoryService.getLevel1Catagorys();
        log.info("总共耗时{}",System.currentTimeMillis()-start);
        model.addAttribute("catagorys", categorys);
        return "index";
    }

    @GetMapping("index/catalog.json")
    @ResponseBody
    public Map<String,List<Catelog2Vo>> getCatalogJson(){
        Map<String, List<Catelog2Vo>> map = categoryService.getCatalogJson();
        return map;
    }

    @GetMapping("/hello")
    @ResponseBody
    public String hello(){
        RLock lock = redissonClient.getLock("my-lock");
        lock.lock();
        try{
            System.out.println("加锁成功"+Thread.currentThread().getName());
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            System.out.println("解锁成功"+Thread.currentThread().getName());
            lock.unlock();
        }
        return "map";
    }

    @GetMapping("/write")
    @ResponseBody
    public String write(){
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("my-lock");
        RLock rLock = readWriteLock.writeLock();
        rLock.lock();
        String s = "";
        try{
            s = UUID.randomUUID().toString();
            Thread.sleep(30000);
            stringRedisTemplate.opsForValue().set("write", s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return s;
    }

    @GetMapping("/read")
    @ResponseBody
    public String read(){
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("my-lock");
        RLock rLock = readWriteLock.readLock();
        rLock.lock();
        String s = "";
        try{
            s = stringRedisTemplate.opsForValue().get("write");
            Thread.sleep(30000);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return s;
    }



}
